﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Handrise.CDCable.Mobile.Business
{
    /// <summary>
    /// 指挥信息服务
    /// </summary>
    public class CommunicationService
    {
        private bool isDownloading = false;

        private Synchronize synchronize = new Synchronize();

        public event DownloadCommunicationCompleteHandler OnDownloadCommunicationComplete;


        public CommunicationService()
        {
            synchronize.OnDownloadCommunicationComplete += new CallWebServiceCompleteHandler(synchronize_OnDownloadCommunicationComplete);
        }

 
        /// <summary>
        /// 处理新接收到的指挥信息
        /// </summary>
        /// <param name="communications"></param>
        public void DealNewReceivedCommunication(List<Object> communications)
        {
            foreach (Communication communication in communications)
            {
                AppLogic.dataService.Add<Communication>(communication);
            }
        }

        /// <summary>
        /// 获取需要上报的指挥信息
        /// </summary>
        /// <param name="userName"></param>
        public List<Communication> GetNeedReportCommunication(string userName)
        {
            return AppLogic.dataService.Find<Communication>("ReportState = '未上报' and Direction ='反馈' and SenderID ='" + userName + "'", "");
        }

        /// <summary>
        /// 通过时间段获取指挥信息
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="startDateTime"></param>
        /// <param name="endDateTime"></param>
        /// <returns></returns>
        public List<Communication> GetCommunicationsByDateTime(string userName,DateTime startDateTime, DateTime endDateTime)
        {
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("StartDateTime", startDateTime);
            parameters.Add("EndDateTime", endDateTime);
            return AppLogic.dataService.Find<Communication>("RecordDateTime >= @StartDateTime and RecordDateTime <= @EndDateTime and (SenderID = '" + userName + "' or ReceiverID = '" + userName + "') ", "RecordDateTime ASC", parameters);
        }

        /// <summary>
        /// 发送指挥信息反馈到主站系统
        /// </summary>
        /// <param name="communication"></param>
        public void SendCommunicationToServer(Communication communication)
        {
            if (AppLogic.netService.CurrentNetType != NetType.None)
            {
                List<Communication> communications = new List<Communication>();
                communications.Add(communication);
                synchronize.UploadCommunication(communications);
            }
        }

        public bool AddCommunication(Communication communication)
        {
            return AppLogic.dataService.Add<Communication>(communication) == 1;
        }

        public bool UpdateCommunication(Communication communication)
        {
            return AppLogic.dataService.Update<Communication>(communication) == 1;
        }

        public void UpdateReportState(List<Communication> communications, string newState)
        {
            //更新上报状态
            foreach (Communication communication in communications)
            {
                communication.ReportState = newState;
                UpdateCommunication(communication);
            }


        }

        /// <summary>
        /// 下载指挥信息
        /// </summary>
        public void DownloadCommunication()
        {
            if (!isDownloading)
            {
                synchronize.DownloadCommunication(AppLogic.LoginUser.UserName);

                isDownloading = true;
            }

        }

        /// <summary>
        /// 下载指挥信息完成事件
        /// </summary>
        /// <param name="info"></param>
        /// <param name="isSuccess"></param>
        /// <param name="userData"></param>
        public void synchronize_OnDownloadCommunicationComplete(string info, bool isSuccess, object userData)
        {
            isDownloading = false;

            if (isSuccess)
            {
                if (OnDownloadCommunicationComplete != null)
                    OnDownloadCommunicationComplete((List<Object>)userData);
            }
        }

    }

    public delegate void DownloadCommunicationCompleteHandler(List<Object> communications);

}
